﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.Plumbing
{
	public partial class FormLayWaterTankNew : System.Windows.Forms.Form
	{
		public FormLayWaterTankNew(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Icon = Resource.YArchitechIcon;
			this.listWaterTank = this.sfdr.GetAllWaterTank("WaterTank");
			this.InitParameterSetting();
		}

		public FormLayWaterTankNew(string name, string spec, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Icon = Resource.YArchitechIcon;
			this.listWaterTank = this.sfdr.GetAllWaterTank("WaterTank");
			this.UpdatePictureBoxAndCombox(this.listWaterTank, name);
			this.comboBox.SelectedItem = spec;
		}

		private void FormLayWaterTankNew_Load(object sender, EventArgs e)
		{
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			List<ListItem> list = new List<ListItem>();
			if (this.listWaterTank.Count > 0)
			{
				foreach (ListItemWaterTank listItemWaterTank in this.listWaterTank)
				{
					if (File.Exists(listItemWaterTank.image))
					{
						list.Add(new ListItem(listItemWaterTank.name, listItemWaterTank.image));
					}
				}
			}
			FormChooseHydrant formChooseHydrant = new FormChooseHydrant(this.name, list);
			formChooseHydrant.Owner = this;
			formChooseHydrant.Text = "选择水箱";
			if (formChooseHydrant.ShowDialog() == DialogResult.OK && this.name != formChooseHydrant.ImageName && formChooseHydrant.ImageName != "")
			{
				this.name = formChooseHydrant.ImageName;
				this.UpdatePictureBoxAndCombox(this.listWaterTank, this.name);
			}
		}

		private void UpdatePictureBoxAndCombox(List<ListItemWaterTank> listWaterTank, string name)
		{
			foreach (ListItemWaterTank listItemWaterTank in listWaterTank)
			{
				if (listItemWaterTank.name == name)
				{
					this.UpdatePictureBox(listItemWaterTank.image);
					this.listWaterTankPara = this.sfdr.GetWaterTankParaById(listItemWaterTank.id, "WaterTankData");
					break;
				}
			}
			this.UpdataCombBoxSPEC(this.listWaterTankPara);
			this.wtName.Text = name;
		}

		private void UpdataCombBoxSPEC(List<ListItemWaterTankPara> lists)
		{
			this.comboBox.Items.Clear();
			if (lists.Count > 0)
			{
				foreach (ListItemWaterTankPara listItemWaterTankPara in lists)
				{
					this.comboBox.Items.Add(listItemWaterTankPara.specname);
				}
				this.comboBox.SelectedIndex = 0;
			}
		}

		private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			foreach (ListItemWaterTankPara listItemWaterTankPara in this.listWaterTankPara)
			{
				if (listItemWaterTankPara.specname == this.comboBox.SelectedItem.ToString())
				{
					this.InitParaTextBox(listItemWaterTankPara);
					this.currentTankPara = listItemWaterTankPara;
					break;
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.tbLevel.Text.Trim() != "")
			{
				this.offset = double.Parse(this.tbLevel.Text);
				this.UserSettingWrite();
				base.DialogResult = DialogResult.OK;
				return;
			}
			YJKMessageBox.Information("标高不能为空，请设置标高");
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void InitParaTextBox(ListItemWaterTankPara item)
		{
			this.wtVolume.Text = item.volume;
			this.wtLength.Text = item.length;
			this.wtWidth.Text = item.width;
			this.wtHeigth.Text = item.height;
			this.wtPipeIn.Text = item.pipein;
			this.wtPipeOut.Text = item.pipeout;
			this.wtPipeOver.Text = item.pipeoverflow;
			this.wtPipeBlow.Text = item.pipeblowoff;
		}

		private void InitParameterSetting()
		{
			this.dic = this.userSetting.GetWaterTankSetting();
			this.name = this.userSetting.GetValueFromDic("name", "装配式钢板给水箱", this.dic);
			this.UpdatePictureBoxAndCombox(this.listWaterTank, this.name);
			this.comboBox.SelectedIndex = int.Parse(this.userSetting.GetValueFromDic("specIndex", "0", this.dic));
			this.tbLevel.Text = this.userSetting.GetValueFromDic("offset", "0.5", this.dic);
		}

		private void UserSettingWrite()
		{
			this.wtName.Text = StringTools.RemoveZero(this.wtName.Text.Trim());
			this.tbLevel.Text = StringTools.RemoveZero(this.tbLevel.Text.Trim());
			try
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("name", this.wtName.Text);
				dictionary.Add("offset", this.tbLevel.Text);
				dictionary.Add("specIndex", this.comboBox.SelectedIndex.ToString());
				this.userSetting.WriteWaterTankSetting(dictionary);
			}
			catch (Exception)
			{
			}
		}

		private void UpdatePictureBox(string image)
		{
			if (File.Exists(image))
			{
				this.pictureBox1.Image = new Bitmap(image);
				return;
			}
			this.pictureBox1.Image = null;
		}

		private void btnMore_Click(object sender, EventArgs e)
		{
			if (FamilyUtil.PathIni.ReadPath(this.doc.Application.VersionNumber) == "")
			{
				YJKMessageBox.Show("请先安装盈建科族立得。");
				return;
			}
			LayFamilyForm layFamilyForm = new LayFamilyForm(new PlumbingDataReader(this.doc).GetFamilyAllInfoTree("水箱"), this.doc);
			if (layFamilyForm.ShowDialog() == DialogResult.OK)
			{
				this.fs = layFamilyForm.fs;
				this.offset = layFamilyForm.OffSet;
				base.DialogResult = DialogResult.OK;
			}
		}

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private HVACUserSetting userSetting = new HVACUserSetting();

		private List<ListItemWaterTank> listWaterTank = new List<ListItemWaterTank>();

		private List<ListItemWaterTankPara> listWaterTankPara = new List<ListItemWaterTankPara>();

		public ListItemWaterTankPara currentTankPara;

		public double offset = 0.5;

		public FamilySymbol fs;

		private Document doc;

		private string name = "";

		private Dictionary<string, string> dic;
	}
}
